/**
 *      example of use :
 *              app.util.getScrollXY()
 *              app.util.getWindowSize()
 *              app.util.scaleImages(100,200,[/*array of images (optional)*\/])
 */
(function () {
  var namespace = namespace || 'app';
  if(typeof (window[namespace]) === 'undefined') {
    return;
  }
  var _getScrollXY = function () {
      var scrOfX = 0;
      var scrOfY = 0;
      if(typeof (window.pageYOffset) == 'number') {
        scrOfX = window.pageXOffset;
        scrOfY = window.pageYOffset;
      } else if(document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        scrOfX = document.body.scrollLeft;
        scrOfY = document.body.scrollTop;
      } else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        scrOfX = document.documentElement.scrollLeft;
        scrOfY = document.documentElement.scrollTop;
      }
      return [scrOfX, scrOfY];
    }
  var _getWindowSize = function () {
      var h;
      var w;
      if(typeof (window.innerWidth) == 'number') {
        w = window.innerWidth;
        h = window.innerHeight;
      } else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        w = document.documentElement.clientWidth;
        h = document.documentElement.clientHeight;
      } else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
        w = document.body.clientWidth;
        h = document.body.clientHeight;
      }
      return [w, h];
    }
  var _scaleImages = function (width, height, images) {
      images = images || document.getElementsByTagName("img");
      for(var i = 0; i < images.length; i++) {
        var ratio = width / height;
        var img = images[i];
        if(img.width / img.height > ratio) {
          if(img.width > width) {
            img.height = Math.round(img.height * (width / img.width));
            img.width = width;
          } else {
            img.width = Math.round(img.width * (height / img.height));
            img.height = height;
          }
        }
      }
    }
  window[namespace].util = {
    getScrollXY: _getScrollXY,
    getWindowSize: _getWindowSize,
    scaleImages: _scaleImages
  }
}());
